# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 -------------------------------------------------
    File Name:     permission.py
    Description:   自定义权限
 -------------------------------------------------
 """
from django.contrib.auth.models import AnonymousUser
from rest_framework.permissions import BasePermission


class CustomPermission(BasePermission):
    """自定义权限"""

    def has_permission(self, request, view):
        """
        判断用户是否有权限访问视图

        :param request:
        :param view:
        :return:
        """
        # 判断用户是否是匿名用户: 匿名用户？未认证的用户
        if isinstance(request.user, AnonymousUser):
            return False
        # IsAuthenticated: 仅允许经过身份验证的用户访问.
        if bool(request.user and request.user.is_authenticated):
            return True
        # 判断用户是否是超级管理员
        if request.user.is_superuser:
            return True
        else:
            return False
